<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Cart;
use EasyWeChat\Factory;
use EasyWeChat\Foundation\Application;
use function EasyWeChat\Kernel\Support\generate_sign;

class CartController extends Controller
{
    public function add(Request $request)
    {
      $params = $request->all();
   // return     $request->input('openid');
// 商品名称(commodity_name) ok
// 商品价格(price)  ok
// 商品id(commodity_id) ok
// 商品图片(img)  ok
// 当前商品所属用户id(user_id)  --
// 购物车当前商品收货地址id(receiving_address_id) --(设置默认地址)
// 给卖家留个言 （默认未填）
// 商品是否选中（0未选中，1选中）（默认选中）
// 商品型号(commodity_series_id) ok
// 购物车商品数量(num) ok
      $cart = new Cart();
      // $code = $request->input('jsCode');
      // $miniProgram = \EasyWeChat::miniProgram();
      // $app = $miniProgram->auth->session($code);
      // $openid = $app['openid'];
      $openid = $request->input('openid');
      $user = User::where('openid', $openid)->get()->toArray();
      $user_id =$user[0]['id'];
      // return $user_id;
      // $user_id = 1;
      $commodity_name = $request->input('commodity_name');
      $price = $request->input('price');
      $commodity_id = $request->input('commodity_id');
      $img = $request->input('img');
      $commodity_series_id = $request->input('commodity_series_id');
      $commodity_series_title = $request->input('commodity_seriesTitle');
      // return $commodity_series_title;
      $num = $request->input('num');


      $cart->commodity_name = $commodity_name;
      $cart->price = $price;
      $cart->commodity_id = $commodity_id;
      $cart->img = $img;
      $cart->commodity_series_id = $commodity_series_id;
      $cart->num = $num;
      $cart->user_id = $user_id;
      $cart->commodity_series_title = $commodity_series_title;
      // echo 99;
      $result = $cart->save();
      // return var_dump($result);
      // echo 9898;
      if($result){
        return response()->json([
              'code' => 200,
              'msg'=>'添加到购物车成功',
          ], 200);
      }else {
        return response()->json([
              'code' => 203,
              'msg'=>'添加到购物车失败!',
          ], 203);
      }
    }

    public function list(Request $request)
    {
      $code = $request->input('jsCode');
      // $miniProgram = \EasyWeChat::miniProgram();
      // $app = $miniProgram->auth->session($code);
      // $openid = $app['openid'];
      $openid = $request->input('openid');
      $user = User::where('openid', $openid)->get()->toArray();
      $user_id =$user[0]['id'];
       // $user_id =20;
       $user = User::find($user_id);
       $data = $user->carts()->paginate(10);
       return $data;
    }

    // 用户删除购物车的商品
    public function delete(Request $request)
    {
      $cart_id = $request->input("id");
      Cart::destroy($cart_id);
    }

    //用户删除购物车的全部商品
    public function deleteAll(Request $request)
    {
      dd($request->all());
      $cart = [4,5];
       Cart::destroy($cart);
    }
}
